<?php

class Calendar_View {
    private $month_num;
    private $year;
    private $resource_type_id;
    
    function __construct($month_num, $year, $resource_type_id ) {
        $this->month_num = $month_num;
        $this->year = $year;
        $this->resource_type_id= $resource_type_id;
    }

    public function toString() {
        $view_wrapper = new Div_Container('id', 'calendar-wrapper');
        $heading = new Div_Container('id', 'View_Heading');
        $body = new Div_Container('id', 'calendar_container_all');
        
        $combo_box_data= '';
        $resource_type_inventory = ResourceTypeDB::getAllResourceTypes();
        if(!is_null($resource_type_inventory))
        {
            $resource_type_inventory->rewind();
            while($resource_type_inventory->valid())
            {
                 $current_item = $resource_type_inventory->current();
                 if($this->resource_type_id == $current_item->getResource_type_id()){
                     
                     $combo_box_data .= '<option value="' . $_SERVER['PHP_SELF'] . '?action=calendar&resource_type_id=' . 
                         $current_item->getResource_type_id()  .'" selected>' . $current_item->getResource_type_name() .'</option>';
                 }
                 else {
                     $combo_box_data .= '<option value="' . $_SERVER['PHP_SELF'] . '?action=calendar&resource_type_id=' . 
                         $current_item->getResource_type_id()  .'">' . $current_item->getResource_type_name() .'</option>';
                 }
                 $resource_type_inventory->next();
            }
           
        }
        
        $combo_box = '<form name="resource_combo">' . 
            '<p><label>Select a resource:</label><select name="resource" class ="styled-select" size="1" onChange="go()">' . 
            $combo_box_data   . '</select></p>';
        
        
        $d1 = new DateTime( $this->year . '-' . $this->month_num . '-1' );
        $d1->modify( 'previous month' );
        $prev_month_year = explode('-', $d1->format('n-Y'));
        
        $d2 = new DateTime( $this->year . '-' . $this->month_num . '-1' );
        $d2->modify( 'next month' );
        $next_month_year = explode('-', $d2->format('n-Y'));
        
        
        
        $next_btn = '<button onclick=window.location.href=' . '\'index.php?action=calendar&do=next&month=' .
                $next_month_year[0] . '&year=' . $next_month_year[1]. '\'' .
                ' type="button" name="next" id="next-btn">Next</button>';
        $prev_btn = '<button onclick=window.location.href=' . '\'index.php?action=calendar&do=prev&month=' .
                $prev_month_year[0] . '&year=' . $prev_month_year[1]. '\'' .
                ' type="button" name="prev" id="prev-btn">Prev</button>';
        $year_month = new Div_Container('id', 'cal_month_year');

        $monthName = date("F", mktime(0, 0, 0, $this->month_num, 10));
        $year_month->addContent($monthName . ', ' . $this->year);
        $buttons = new Div_Container('id', 'next-prev-month-btn');
        $buttons->addContent($prev_btn . $next_btn);
        $resource_combo = new Div_Container('id', 'resource-combo');
        $resource_combo->addContent($combo_box);

        $heading->addContent($year_month->toString() . $buttons->toString() . $resource_combo->toString());
        
        $num_days = cal_days_in_month(CAL_GREGORIAN, $this->month_num, $this->year);
        $today_date = date("j F Y");  
        $today_timestapm = strtotime($today_date);
        
        $available = 0;
        for ($i = 1; $i <= $num_days; $i++) {
            $cal_date = $i . '-' . $this->month_num . '-' . $this->year;
            $cal_date = new DateTime( $cal_date );
            
            $cal_date_timestamp = strtotime($cal_date->format("j F Y"));
            $slot_date = $cal_date->format("Y-m-d");
            $available = getAvailableResourcesCount($slot_date, $this->resource_type_id);
            if($today_timestapm <= $cal_date_timestamp)
            {
                $day = new Cal_Day_Container($i, $this->month_num, $this->year, $available, $_SERVER['PHP_SELF'] . '?action=schedule&day=' . 
                    $i . '&month=' . $this->month_num . '&year=' . $this->year . '&resource_type_id=' . $this->resource_type_id);
            }
            else{
                $available = 0;
                $day = new Cal_Day_Container($i, $this->month_num, $this->year, $available, $_SERVER['PHP_SELF'] . '?action=schedule&day=' . 
                    $i . '&month=' . $this->month_num . '&year=' . $this->year . '&resource_type_id=' . $this->resource_type_id, 'yes');
            }
            $body->addContent($day->toString());
        }

        $view_wrapper->addContent($heading->toString());
        $view_wrapper->addContent($body->toString());

        return $view_wrapper->toString();
    }

}

?>
